<?php if (!defined("_ECRIRE_INC_VERSION")) return; if (class_exists('AlohaCart')) return;
/**
 * Aloha Cart
 *
 * @author     Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com)
 * @license    GNU/GPL
 * @package    plugins
 * @subpackage alohacart
 * @copyright  (c) Aleksey Yaroshchuk 2011-2012
 * @link       http://www.spip-contrib.net/AlohaCart
 */

/**
 *  Class that represents shopping Cart
 *
 *  @author Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com) 
 */
class AlohaCart {
    /**
     * Session variable for the Cart
     */
    var $_session_data;
    
    public function __construct() {
        
        // We'll use standart PHP session. But in future we can change it to SPIP session
        @session_start();
        if (!isset($_SESSION["alohacart"]) || !is_array($_SESSION["alohacart"])) {
      		$_SESSION["alohacart"] = array();
    	}
        $this->_session_data = &$_SESSION["alohacart"];
    }
    
    /**
     *  Get array of products that are located in the Cart 
     */
    public function GetProducts() {
        return $this->_session_data;
    }
    
    /**
     *  Remove all products from the Cart
     */
    public function ClearCart() {
		$this->_session_data = array();
  	}
    
    /**
     *  Count all products in the Cart
     *  @return Number of products in the Cart
     */
    public function CountProducts() {
		$cart_num = 0;
        foreach ($this->_session_data as $product) {
            $cart_num += $product["quantity"];
        }
        return $cart_num;         
	}
    
    /**
     *  Count total price of products in the Cart
     *  @return Number of products in the Cart
     */
    public function CountPrice() {
		$cart_price = 0;
        foreach ($this->_session_data as $product) {
            $cart_price += $product["price"];
        }
        return $cart_price;         
	}
    
    /**
     *  Add product to cart
     */
    public function AddProduct($id_product, $product_data) { 
        if (!isset($product_data) || !is_array($product_data) || empty($product_data)) return;
        if (!(int)$product_data["quantity"] || (int)$product_data["quantity"]<=0) return;        
        if (!isset($this->_session_data[$id_product])) {
            $this->_session_data[$id_product] = $product_data;
	    } else {
	        $this->_session_data[$id_product]["quantity"] += $product_data["quantity"]; 
            $this->_session_data[$id_product]["price"] += $product_data["quantity"]*$this->_session_data[$id_product]["price_one"];
	    }
  	}
    
    /**
     *  Remove product from the Cart
     */
    public function RemoveProduct($id_product) {
        unset($this->_session_data[$id_product]);
    }
    
    /**
     *  Update product in the Cart
     */    
    public function UpdateProduct($id_product, $product_data) {
        if (!isset($product_data) || !is_array($product_data) || empty($product_data)) return;
        foreach ($product_data as $property=>$value) {
            $this->_session_data[$id_product][$property] = $value;    
        }
        
    }
} 
?>